package com.supermap.gwfs.datashare.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.supermap.gcpp.core.common.UniObject;


//读取配置文件（属性文件）的工具类
public class ReadXML {
	private Logger logger = Logger.getLogger(this.getClass());
	private static UniObject paramUniObject = null;
	private static Map<String, Integer> eleIntervalMap = null;
	private static ReadXML readXML = null;
	private ReadXML() {
		paramUniObject = this.readXml();
		eleIntervalMap = this.readXmlByElementInterval();
	}
	
	public static ReadXML getInstance()
	{
		if (readXML == null)
		{
			readXML = new ReadXML();
		}
		return readXML;
	}
	
	public UniObject getParameter()
	{
		return paramUniObject;
	}
	
	public Map<String, Integer> getEleIntervalMap()
	{
		return eleIntervalMap;
	}
 	
	public UniObject readXml()
	{
		UniObject uniObject = new UniObject("meteorologcal");
		FileInputStream fis = null;
		try {
			SAXReader reader = new SAXReader();
			fis = new FileInputStream(new File(ReadXML.class.getClassLoader().getResource("weather.xml").getPath()));
			Document doc = reader.read(fis);
			Element root = doc.getRootElement();
			
			
			//获取初始场信息
			Element originElement = root.element("originElement");
			List<Element> originList = originElement.elements("origin");
			UniObject originObject = new UniObject("origin");
			for (Element e : originList)
			{
				String id = e.attributeValue("id");
				String value = e.attributeValue("value");
				String special = e.attributeValue("special");
				String version = e.attributeValue("version");
				String element = e.getStringValue();
				UniObject u = new UniObject(value);
				u.setValue("originId", id);
				u.setValue("originValue", value);
				u.setValue("special", special);
				u.setValue("version", version);
				u.setValue("elements", element);
				originObject.addContainedObject(u);
			}
			
			//天气现象判断标准
			Element judment = root.element("judmentstandard");
			List<Element> statandars = judment.elements("standard");
			UniObject statandarObject = new UniObject("statandar");
			for (Element s : statandars)
			{
				String interval = s.attributeValue("interval");
				String name = s.attributeValue("name");
				String value = s.attributeValue("value");
				String code = s.attributeValue("code");
				String statandar = s.getStringValue();
				UniObject u = new UniObject(name);
				u.setValue("statandarInterval", interval);
				u.setValue("statandarName", name);
				u.setValue("statandarValue", value);
				u.setValue("statandar", statandar);
				u.setValue("code", code);
				statandarObject.addContainedObject(u);
			}
			
			
			Element weather = root.element("weathers");
			List<Element> weathers = weather.elements("weather");
			UniObject weatherObject = new UniObject("weather");
			for (Element w : weathers)
			{
				String weatherCode = w.attributeValue("code");
				String weatherPriority =  w.attributeValue("priority");
				String weatherString = w.getStringValue();
				UniObject u = new UniObject(weatherCode);
				u.setValue("weatherCode", weatherCode);
				u.setValue("weatherPriority", weatherPriority);
				u.setValue("weather", weatherString);
				weatherObject.addContainedObject(u);
				
			}
			
			uniObject.addContainedObject(originObject);
			uniObject.addContainedObject(statandarObject);
			uniObject.addContainedObject(weatherObject);
			
		}
		catch (Exception e) {
			logger.error("ZJ:Analytical XML file error , error : " + e);
		}
		finally
		{
			try
			{
				if (fis != null)
				{
					fis.close();
				}
			}
			catch (IOException e)
			{
				logger.error("ZJ: IO closed error , error : " + e);
			}
		}
		return uniObject.clone();
	}
	
	
	public Map<String, Integer> readXmlByElementInterval()
	{
		Map<String, Integer > eleIntervalMap = new HashMap<String, Integer>();
		FileInputStream fis = null;
		try {
			SAXReader reader = new SAXReader();
			fis = new FileInputStream(new File(ReadXML.class.getClassLoader().getResource("interval.xml").getPath()));
			Document doc = reader.read(fis);
			Element root = doc.getRootElement();
			
			List<Element> list = root.elements();
			for (Element e : list)
			{
				String element = e.attributeValue("value");
				String interval = e.attributeValue("interval");
				eleIntervalMap.put(element, Integer.valueOf(interval));
			}
			
		}
		catch (Exception e) {
			logger.error("ZJ:Analytical XML file error , error : " + e);
		}
		finally
		{
			try
			{
				if (fis != null)
				{
					fis.close();
				}
			}
			catch (IOException e)
			{
				logger.error("ZJ: IO closed error , error : " + e);
			}
		}
		return eleIntervalMap;
	}
	
	
	
	
	
	public static void main(String[] args)
	{
//		UniObject uniObject = new ReadXML().readXml();
//		System.out.println(uniObject);
		
		new ReadXML().readXmlByElementInterval();
	}

}
